﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ScriptCoreLib;
using ScriptCoreLib.Shared.Avalon.Extensions;
using ScriptCoreLib.Shared.Lambda;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Input;

namespace AvalonTycoonMansion.Shared.Promotion
{
    [Script]
    public class GameSocialLinksButton
    {
        public ImageSource Source;

        public int Width;
        public int Height;

        public Action Click;

        public Uri Hyperlink;

        internal Image Image;
        internal Rectangle Overlay;

        public int X;
        public int Y;
    }

    [Script]
    public class GameSocialLinks : ISupportsContainer, IEnumerable<GameSocialLinksButton>
    {
        public Canvas Container { get; set; }



        public readonly BindingList<GameSocialLinksButton> Buttons = new BindingList<GameSocialLinksButton>();

        public int Margin = 8;

        public int OffsetY
        {
            set
            {
                foreach (var e in Buttons)
                {
                    e.Image.MoveTo(e.X, e.Y + value);
                    e.Overlay.MoveTo(e.X, e.Y + value);
                }
            }
        }
        public GameSocialLinks(Canvas Container)
        {
            this.Container = Container;

            Buttons.ForEachNewItem(
                e =>
                {
                    var i = true;
                    //var x = Buttons.Where(k => i).Aggregate(Margin,
                    //    (s, k) =>
                    //    {
                    //        if (k == e)
                    //        {
                    //            i = false;
                    //            return s;
                    //        }

                    //        return s + k.Width + Margin;
                    //    }
                    //);

                    //var y = this.Container.Height - Margin - e.Height;

                    e.Y = Buttons.Where(k => i).Aggregate(Margin,
                        (s, k) =>
                        {
                            if (k == e)
                            {
                                i = false;
                                return s;
                            }

                            return s + k.Height + Margin;
                        }
                    );


                    e.X = Margin;

                    e.Image = new Image
                    {
                        Source = e.Source,
                        Width = e.Width,
                        Height = e.Height,
                        Name = "GameSocialLinks_Button_Image",
                        Opacity = 0.6
                    }.AttachTo(this).MoveTo(e.X, e.Y);

                    e.Overlay = new Rectangle
                    {
                        Fill = Brushes.White,
                        Width = e.Width,
                        Height = e.Height,
                        Opacity = 0,
                        Cursor = Cursors.Hand,
                        Name = "GameSocialLinks_Button_Overlay",
                    }.AttachTo(this).MoveTo(e.X, e.Y);

                    var ImageOpacity = e.Image.ToAnimatedOpacity();
                    ImageOpacity.Opacity = 0.4;

                    e.Overlay.MouseEnter +=
                        delegate
                        {
                            ImageOpacity.Opacity = 1;
                        };

                    e.Overlay.MouseLeave +=
                        delegate
                        {
                            ImageOpacity.Opacity = 0.4;
                        };

                    e.Overlay.MouseLeftButtonUp +=
                        delegate
                        {
                            if (e.Click != null)
                                e.Click();

                            if (e.Hyperlink != null)
                                e.Hyperlink.NavigateTo();
                        };
                }

            );
        }

        public void Add(GameSocialLinksButton e)
        {
            this.Buttons.Add(e);
        }

        #region IEnumerable<Button> Members

        public IEnumerator<GameSocialLinksButton> GetEnumerator()
        {
            return this.Buttons.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.Buttons.GetEnumerator();
        }

        #endregion
    }
}
